package cn.iocoder.yudao.module.wms.controller.admin.rdrecord01;

import cn.iocoder.yudao.module.wms.controller.admin.pomain.vo.PomainRespVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.pomain.PomainDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.rdrecord01.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecord01.Rdrecord01DO;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecord01.Rdrecords01DO;
import cn.iocoder.yudao.module.wms.service.rdrecord01.Rdrecord01Service;

@Tag(name = "管理后台 - 采购入库主")
@RestController
@RequestMapping("/wms/rdrecord01")
@Validated
    public class Rdrecord01Controller {

    @Resource
    private Rdrecord01Service rdrecord01Service;

    @PostMapping("/create")
    @Operation(summary = "创建采购入库主")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord01:create')")
    public CommonResult<Integer> createRdrecord01(@Valid @RequestBody Rdrecord01SaveReqVO createReqVO) {
        return success(rdrecord01Service.createRdrecord01(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新采购入库主")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord01:update')")
    public CommonResult<Boolean> updateRdrecord01(@Valid @RequestBody Rdrecord01SaveReqVO updateReqVO) {
        rdrecord01Service.updateRdrecord01(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除采购入库主")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:rdrecord01:delete')")
    public CommonResult<Boolean> deleteRdrecord01(@RequestParam("id") Integer id) {
        rdrecord01Service.deleteRdrecord01(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得采购入库主")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord01:query')")
    public CommonResult<Rdrecord01RespVO> getRdrecord01(@RequestParam("id") String id) {
        try {
            Rdrecord01DO rdrecord01 = rdrecord01Service.getRdrecord01(Integer.valueOf(id));
            return success(BeanUtils.toBean(rdrecord01, Rdrecord01RespVO.class));

        }catch (Exception e)
        {
            Rdrecord01DO  rdrecord01=  rdrecord01Service.findByProcessInstanceId(id);
            return success(BeanUtils.toBean(rdrecord01, Rdrecord01RespVO.class));
        }
    }

    @GetMapping("/startProcess")
    @Operation(summary = "启动流程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:rdrecord01:create')")
    public CommonResult<Boolean> startProcess(@RequestParam("id") Integer id) {
        rdrecord01Service.process(id);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得采购入库主分页")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord01:query')")
    public CommonResult<PageResult<Rdrecord01RespVO>> getRdrecord01Page(@Valid Rdrecord01PageReqVO pageReqVO) {
        PageResult<Rdrecord01DO> pageResult = rdrecord01Service.getRdrecord01Page(pageReqVO);
        return success(BeanUtils.toBean(pageResult, Rdrecord01RespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出采购入库主 Excel")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord01:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRdrecord01Excel(@Valid Rdrecord01PageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<Rdrecord01DO> list = rdrecord01Service.getRdrecord01Page(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "采购入库主.xls", "数据", Rdrecord01RespVO.class,
                        BeanUtils.toBean(list, Rdrecord01RespVO.class));
    }

    // ==================== 子表（采购入库子） ====================

    @GetMapping("/rdrecords01/list-by-pid")
    @Operation(summary = "获得采购入库子列表")
    @Parameter(name = "pid", description = "主表ID")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord01:query')")
    public CommonResult<List<Rdrecords01RespVO>> getRdrecords01ListByPid(@RequestParam("pid") Integer pid) {
        return success(rdrecord01Service.findRdrecords01ByPid(pid));
    }

}